home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 356 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: lrz-muenchen.de!sun2!ua302aa
  2. From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: start array at k, not 0
  5. Date: 4 Jan 1996 19:20:23 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4ch99n$ick@sparcserver.lrz-muenchen.de>
  9. References: <Pine.OSF.3.91.960104095358.22268B-100000@io.UWinnipeg.ca>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. Bill Simpson <wsimpson@uwinnipeg.ca> writes:
  13.  
  14. >I have come up with the following 2 methods that allow one to talk about
  15. >an array that starts at element k rather than 0.  E.g. 10 element array
  16. >y[k] to y[k+10].  Both seem to work.  Is this illusory?  Is one of the
  17. >2 ways better (or a way I haven't mentioned)?
  18.  
  19. >These programs set up y[5] to y[14].
  20.  
  21. >Thanks very much for any comments.
  22.  
  23. >Bill Simpson
  24.  
  25. >/*method 1*/
  26. >#include <stdio.h>
  27.  
  28. >int main(void)
  29. > {
  30. > int i, x[10];
  31. > int* y;
  32. > int offset=5;  /*ie 1st index at 5, y[5]-y[14] */
  33. > y=x-offset;
  34.  
  35. You performed pointer arithmetic that led you outside array bounds,
  36. so from now on anything can happen. "x - 1 < x" need not be true.
  37.  
  38. > printf("offset=%d\n",offset);
  39.  
  40. > for (i=offset;i<10+offset;i++)
  41. >        {
  42. >        y[i]=i;
  43. >        printf("%d\n",y[i]);
  44. >        }
  45. > return 0;
  46. > }
  47.  
  48. >/*method 2*/
  49. >#include <stdio.h>
  50. >#include <stdlib.h>
  51.  
  52. >int main(void)
  53. > {
  54. > int i;
  55. > int* y;
  56. > int offset=5;  
  57. > int n=10;       
  58. > y=malloc(n*sizeof(int));
  59.  
  60. > printf("offset=%d\n",offset);
  61.  
  62. > for (i=offset;i<n+offset;i++)
  63. >        {
  64. >        y[i]=i;
  65. >        printf("%d\n",y[i]);
  66. >        }
  67. > return 0;
  68. > }
  69.  
  70. Now you access memory that has not been allocated, or may memory 
  71. that has been allocated, but for a different purpose. While pointer
  72. arithmetic referring to "x + 10" is well defined, storing anything
  73. through that pointer is not.
  74.  
  75. If you absolutely _have to_ have arrays with a positive offset
  76. greater than 0 in C, allocate the memory you need and waste
  77. some bytes at the beginning of the array:
  78.  
  79.    int y[15];
  80.  
  81. Now you can do whatever you want with y[5], ..., y[14].
  82.  
  83. BTW, in most cases it is possible to work with 0 based arrays,
  84. imho. If you absolutely need a different base, try to hide
  85. access to array elements behind a set of macros that take your
  86. offset into account.
  87.  
  88. Kurt
  89. --
  90. | Kurt Watzka                             Phone : +49-89-2180-6254
  91. | watzka@stat.uni-muenchen.de
  92. | ua302aa@sunmail.lrz-muenchen.de
  93.